﻿Public Partial Class AddProduct
    Inherits System.Web.UI.Page

    Public product As Domain.dnc_Product
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        'If Not Page.IsPostBack Then
        '    txtStartDate.Text = Date.Now.ToString("dd/MM/yyy
        'End If

        ' Get Product
        Dim manager As New BLL.ProductManager

        product = manager.GetProductByZoneId(Convert.ToInt32(Session.Item("ZoneId")))
       
        lblZone.Text = product.Name
        lblDescription.Text = product.Description
        lblCost.Text = String.Format("${0} per month", product.Cost)
    End Sub

    Protected Sub btnPurchase_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPurchase.Click

        
        ' Only proceed if all validation passes
        If Page.IsValid Then

            'TODO: Add your own paypal link details here
            ' You can make up all the details as long as your email address is valid and registered with paypal
            ' and that you choose a valid currency code
            Dim email As String = HttpUtility.UrlEncode("adam@dotnetcore.com")
            Dim productName As String = HttpUtility.UrlEncode(String.Format("Banner Management - {0} - {1} months", product.Name, ddlMonth.SelectedValue.ToString))
            Dim itemNumber As String = HttpUtility.UrlEncode("11")
            Dim price As String = Convert.ToString(Convert.ToInt32(ddlMonth.SelectedValue) * product.Cost)
            Dim currencyCode As String = "AUD" 'Example: USD or AUD

            ' Redirect
            Response.Redirect(String.Format("https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business={0}&item_name={1}&item_number={2}&amount={3}&shipping=0%2e00&no_shipping=0&no_note=1&currency_code={4}&lc=AU&bn=PP%2dBuyNowBF&charset=UTF%2d8", email, productName, itemNumber, price, currencyCode))

        End If

    End Sub

#Region " Validation "

    Private Sub cvBanner_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles cvBanner.ServerValidate

        args.IsValid = True

        'Dim result As DateTime

        '' Validate date
        'If Not DateTime.TryParseExact(txtStartDate.Text, "dd/MM/yyyy", Nothing, Globalization.DateTimeStyles.None, result) Then
        '    args.IsValid = False
        '    args()
        'End If

        ' Get Zone Information

        Dim zoneManager As New BLL.ZoneManager

        Dim zone As Domain.dnc_Zone = zoneManager.GetZoneByID(product.ZoneId)

        Dim zoneImage As Domain.dnc_ZoneImage = zoneManager.GetZoneImageByID(zone.ZoneImageId)


        Dim intLength As Integer

        Dim arrContent As Byte()

        If fupBanner.PostedFile.ContentLength = 0 Then

            cvBanner.ErrorMessage = "You must upload an Advertisement Image"
            args.IsValid = False
            Exit Sub
        Else

            Dim fileName As String = fupBanner.PostedFile.FileName

            Dim ext As String = fileName.Substring(fileName.LastIndexOf("."))

            ext = ext.ToLower.Replace(".", "")

            If Not (ext = "png" Or ext = "jpg" Or ext = "gif") Then
                cvBanner.ErrorMessage = "The image type is not supported. Only gif, jpg or pngs are supported"
                args.IsValid = False
                Exit Sub
            End If
        End If



        intLength = Convert.ToInt32(fupBanner.PostedFile.InputStream.Length)

        ReDim arrContent(intLength)

        fupBanner.PostedFile.InputStream.Read(arrContent, 0, intLength)

        Dim UploadedImage As System.Drawing.Image = System.Drawing.Image.FromStream(fupBanner.PostedFile.InputStream)

        ' Checks that the uploaded image is the correct size for the zone
        If Not UploadedImage.Height = Convert.ToInt32(zoneImage.ImageHeight) Or Not UploadedImage.Width = Convert.ToInt32(zoneImage.ImageWidth) Then
            cvBanner.ErrorMessage = String.Format("The image must be {0} x {1} in size to fit in this zone. Please change your banner image size", zoneImage.ImageWidth, zoneImage.ImageHeight)
            args.IsValid = False
            Exit Sub
        End If

        If Page.IsValid Then
            Dim manager As New DotNetCore.BannerManagement.BLL.ImageManager

            Dim img As New DotNetCore.BannerManagement.Domain.dnc_Image

            img.Image = arrContent
            img.ContentType = fupBanner.PostedFile.ContentType
            img.Name = txtName.Text
            img.Width = UploadedImage.Width
            img.Height = UploadedImage.Height

            ' Insert image
            manager.InsertImage(img)

            ' Insert Banner Advertisement
            Dim advertiserManager As New BLL.AdvertiserManager
            Dim advertiser As Domain.dnc_Advertiser = advertiserManager.GetAdvertiser(BLL.UserManager.GetInstance.GetUsername)
            Dim adManager As New DotNetCore.BannerManagement.BLL.AdvertisementManager

            Dim advertisement As New Domain.dnc_Advertisement
            advertisement.Active = False
            advertisement.AdvertiserID = advertiser.ID
            advertisement.AlternateText = txtAltText.Text
            advertisement.EndDate = Date.Now.AddMonths(CInt(ddlMonth.SelectedValue))
            advertisement.startDate = Date.Now
            advertisement.ImageID = img.ID
            advertisement.Name = txtName.Text
            advertisement.URL = txtURL.Text

            adManager.InsertAdvertisement(advertisement)

            'Add to Advertisement Zone
            Dim adZone As New Domain.dnc_AdvertisementZone
            adZone.AdvertisementID = advertisement.ID
            adZone.ZoneID = zone.ID

            adManager.InsertAdvertisementZone(adZone)
        End If

    End Sub

#End Region


End Class